#define __uart_c

#include "includeAll.h"





void TIMER1_setup()
{
    //产生9600的串口通信波特率
    TMOD&=0xcf;
    TMOD|=0x20;//                              T1设置为8位自动重载定时器
    TL1=(256-16);
    TH1=(256-16);
    TF1=0;//                                  清空T1中断标志
    TR1=1;//                                  启动定时器T1
    ET1=0;//                                  禁止T1中断
}

void UART_setup()
{
    P3MODL&=0xf0;
    P3MODL|=0x09;
    P3|=0x03;

    PCON=0x80;//                         允许UART双波特率
    OPTION=0x30;//                       禁止一线UART模式 WDT=50ms ADCLK=FSYSCLK/32
    SCON=0x50;//                         串口设置为模式1:8位UART,波特率可变,允许接收
    TI=0;
    RI=0;
    ES=1;
}

void UART_int() interrupt 4
{
    if(TI)
    {
        TI=0;//                                clear interrupt flag
        UARTFaSong();
    }
    if(REN && RI)
    {
        RI=0;//                                clear interrupt flag
      UARTJieShou();
    }
}


void UARTFaSong()
{
    if((bSend)&&(step<47))
    {
            if(TI==0)
            {
                step++;
                if(step==1)
                {
                    yiHuo1=sendBuff[1]^sendBuff[0];
                }
                else
                {
					// if(step==6)
					// {
						// sendBuff[6]=sendBuff[6]|0x80;
					// }
					// if((step>6)&&(step<37))
					// {
						// sendBuff[step]=0;
					// }
                    if(step!=47)
                    {
                        yiHuo1=yiHuo1^sendBuff[step];
                    }
                }
                if(step==47)
                {
                    sendBuff[47]=yiHuo1;
					bSend=0;
                }
				
                SBUF=sendBuff[step];
            }
    }	
}	
void  UARTJieShou()
{
    if(SBUF==0x5a)
    {
        step=0;
    }
    if(step==0)
    {
        yiHuo=SBUF;
    }
    else
    {
        if(step!=finishStep)
        {
            yiHuo=yiHuo^SBUF;
        }
        if(finishStep==step)
        {
            if(yiHuo!=SBUF)
            {
                uartBuff[0]=0;
                uartBuff[1]=0;
                uartBuff[2]=0;
                uartBuff[3]=0;
                uartBuff[4]=0;
                uartBuff[5]=0;
                uartBuff[6]=0;
            }
			else
			{
				holdLostWifiTime=0;
			}
            finishStep=0;
            step=0;
            yiHuo=0;
            bFinish=1;
		
        }
    }

    switch(step)
    {
    case 0:
        if(SBUF==0x5a)step++;
        break;

    case 1:
        if(SBUF==0xa5)step++;
        break;

    case 2:
        if(SBUF==0x05)step++;
        break;

    case 3:
        if(SBUF==0x02)step++;
        break;

    case 4:
        uartBuff[0]=SBUF;//命令类型TYPE
        step++;
        break;

    case 5:
        uartBuff[1]=SBUF;//数据长度LEN
        finishStep=SBUF+step+1;
        step++;
        break;

    case 6:
        uartBuff[2]=SBUF;  //data0
        step++;
        break;

    case 7:
        uartBuff[3]=SBUF;  //data1
        step++;

        break;

    case 8:
        uartBuff[4]=SBUF;  //data2
        step++;
        break;

    case 9:
        uartBuff[5]=SBUF;  //data3
        step++;
        break;

    case 10:
        uartBuff[6]=SBUF;  //data4
        step++;
        break;

    case 11:
        uartBuff[7]=SBUF;  //data5
        step++;
        break;
	
    case 12:
        uartBuff[8]=SBUF;  //data6
        step++;
        break;
	
	case 13:
        uartBuff[9]=SBUF;  //data7
        step++;
        break;
	
	case 14:
        uartBuff[10]=SBUF;  //data8
        step++;
        break;
	
	
	case 15:
        uartBuff[11]=SBUF;  //data9
        step++;
        break;
		
	case 16:
        uartBuff[12]=SBUF;  //data10
        step++;
        break;
		
	case 17:
        uartBuff[13]=SBUF;  //data11
        step++;
        break;
		
	case 18:
        uartBuff[14]=SBUF;  //data12
        step++;
        break;
		
	case 19:
        uartBuff[15]=SBUF;  //data13
        step++;
        break;
	
	case 20:
        uartBuff[16]=SBUF;  //data14
        step++;
        break;
	
	case 21:
        uartBuff[17]=SBUF;  //data15
        step++;
        break;
		
	
    }

    if(bFinish)
    {
        bFinish=0;
        switch(uartBuff[0])
        {
        case 0x01:
            if(uartBuff[2]==0x01)
            {
                bPowerOnOff=0;
                keyOnOff();
            }
            else if(uartBuff[2]==0x00)
            {
                bPowerOnOff=1;
                keyOnOff();
            }

            break;

        case 0x02:
            if(uartBuff[2]==0x0d)
            {
                dangWeiNum=2;
                KeyDangWei();
            }
            if(uartBuff[2]==0x0e)
            {
               dangWeiNum=3;
                KeyDangWei();
            }
            else if(uartBuff[2]==0x0f)
            {
                 dangWeiNum=1;
                KeyDangWei();
            }
            break;

        case 0x03:
            if(uartBuff[2]==0x01)
            {
                gear=1;
                DianReZhuangTai1000W;
            }
            else if(uartBuff[2]==0x02)
            {
                gear=2;
                DianReZhuangTai2000W;
            }
            else if(uartBuff[2]==0x03)
            {
                gear=3;
                DianReZhuangTai3000W;
            }
            break;

        case 0x04:
			
            switch(uartBuff[2])
            {
				
            case 0x1e:
			Buzz(25);
              setCurTemp=30;
             KeyTiaoWen2();
                break;

            case 0x23:
			Buzz(25);
               setCurTemp=35;
               KeyTiaoWen2();
                break;

            case 0x28:
			Buzz(25);
                setCurTemp=40;
                KeyTiaoWen2();
                break;

            case 0x2d:
			Buzz(25);
               setCurTemp=45;
                KeyTiaoWen2();
                break;

            case 0x32:
			Buzz(25);
                setCurTemp=50;
               KeyTiaoWen2();
                break;

            case 0x37:
			Buzz(25);
                setCurTemp=55;
                KeyTiaoWen2();
                break;

            case 0x3c:
			Buzz(25);
               setCurTemp=60;
                KeyTiaoWen2();
                break;

            case 0x41:
			Buzz(25);
               setCurTemp=65;
                KeyTiaoWen2();
                break;

            case 0x46:
			Buzz(25);
                setCurTemp=70;
               KeyTiaoWen2();
                break;

            case 0x4b:
			Buzz(25);
               setCurTemp=75;
                KeyTiaoWen2();
                break;
            }
			
            break;

        case 0x05:
            if(uartBuff[2]==0x01)
            {
                Buzz(25);
                bOrderClose=0;
                bOrderOpen=1;
            }
            else if(uartBuff[2]==0x00)
            {
                Buzz(25);
                bOrderClose=0;
                bOrderOpen=0;
            } 
			orderClockOpenHour=uartBuff[3];
            orderClockOpenMin=uartBuff[4];
			orderClockCloseHour=uartBuff[5];
            orderClockCloseMin=uartBuff[6];
            break;

        case 0x06:
			Buzz(25);
			if((uartBuff[2]&0x01)!=0)
			{
				bYuYue1=1;
			}
			else
			{
				bYuYue1=0;
			}
		    if((uartBuff[2]&0x02)!=0)
			{
				bYuYue2=1;
			}
			else
			{
				bYuYue2=0;
			}
			 if((uartBuff[2]&0x04)!=0)
			{
				bYuYue3=1;
			}
			else
			{
				bYuYue3=0;
			}
			
			
			
			orderClockOpenHour1=uartBuff[3];
			orderClockOpenMin1=uartBuff[4];
			orderClockCloseHour1=uartBuff[5];
			orderClockCloseMin1=uartBuff[6];
			
			orderClockOpenHour2=uartBuff[7];
			orderClockOpenMin2=uartBuff[8];
			orderClockCloseHour2=uartBuff[9];
			orderClockCloseMin2=uartBuff[10];
			
			orderClockOpenHour3=uartBuff[11];
			orderClockOpenMin3=uartBuff[12];
			orderClockCloseHour3=uartBuff[13];
			orderClockCloseMin3=uartBuff[14];
			
			yuYueWenDu1=uartBuff[15];
			yuYueWenDu2=uartBuff[16];
			yuYueWenDu3=uartBuff[17];
          
            break;

        case 0x07:

            // orderClockCloseHour=uartBuff[2];
            // orderClockCloseMin=uartBuff[3];
            break;

        case 0x08:

            break;

        case 0x09:
            break;

        case 0xfd:
		//Buzz(25);
          bUpdateTime=1;
		//	mainClockHour=uartBuff[5];
		//	mainClockMin=uartBuff[6];
			
            break;

        case 0xfe:

            bSend=1;
          wifiNum=uartBuff[2];
		
            data32=wifiNum;;

            break;

        case 0xff:

           wifiNum=uartBuff[2];
		     
            data32=wifiNum;
            break;
        }
    }

}